﻿using System;
using Caliburn.Micro;
using SS.WPF.Framework.Restriction;

namespace SS.WPF.Toolkit.PropertyEditor
{
    /// <summary>
    /// The class is used to define a item in editor
    /// <para>
    /// The Item class implement interface PropertyChangedBase, 
    /// so it will be notified when value on destination has been changed
    /// </para>
    /// </summary>
    public abstract class Item : PropertyChangedBase, IDisposable
    {
        #region Properties & Fields

        /// <summary>
        /// The property is used to apply restrictions for Item
        /// <para>
        /// The rule of restriction is defined by user
        /// </para>
        /// </summary>
        private IRestriction _restriction;
        public IRestriction Restriction
        {
            get
            {
                return this._restriction;
            }
            set
            {
                this._restriction = value;
                this.NotifyOfPropertyChange(() => this.Restriction);
            }
        }

        private bool _disposed;
        protected bool Disposed
        {
            get
            {
                return this._disposed;
            }
        }

        #endregion

        #region Constructors and Destructors

        ~Item()
        {
            this.Dispose(false);
        }

        #endregion

        #region Implementation of IDisposable

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.Disposed)
            {
                this._disposed = true;
            }
        }

        #endregion
    }
}
